
const userService = require('../../service/admin/userService')

const JWT = require('../../utils/JWT')
const userController = {
    login: async (req, res) => {
        console.log(req.body)
        var result = await userService.login(req.body)
        console.log(result)
        if (result.length === 0) {
            res.send({
                code: '-1',
                error: '用户名密码不匹配'
            })
        } else {
            const token = JWT.generate({
                _id: result[0]._id,
                username: result[0].username
            }, '2400s')

            res.header('Authorazation', token)
            res.send({
                actionType: 'ok',
                data: {
                    _id: result[0]._id,
                    username: result[0].username,
                    gender: result[0].gender ? result[0].gender : 0,   //性别 0 1 2
                    introduction: result[0].introduction,
                    avatar: result[0].avatar,
                    role: result[0].role,   //管理员 1，编辑2}
                }
            })
            global.userId = result[0]._id
        }
    },

    upload: async (req, res) => {
        console.log(req.body, req.file)
        const { username, introduction, gender } = req.body
        const avatar = req.file ? `/avataruploads/${req.file.filename}` : ''
        const token = req.headers.authorazation.split(' ')[1]
        var payload = JWT.verify(token)
        console.log(payload)
        // 调用service模块更新 数据

        await userService.upload({ _id: payload._id, username, introduction, gender: Number(gender), avatar })
        console.log(avatar)
        if (avatar) {
            res.send({
                ActionType: 'ok',
                data: {
                    username,
                    introduction,
                    gender: Number(gender),
                    avatar
                }
            })
        } else {
            res.send({
                ActionType: 'ok',
                data: {
                    username,
                    introduction,
                    gender: Number(gender)
                }
            })
        }

    },

    add: async (req, res) => {
        console.log(req.body, req.file)
        const { username, introduction, gender, role, password } = req.body
        const avatar = req.file ? `/avataruploads/${req.file.filename}` : ''
        // 调用service模块更新 数据

        await userService.add({ username, introduction, gender: Number(gender), role: Number(role), avatar, password })
        res.send({
            ActionType: 'ok',
        })

    },

    getList: async (req, res) => {
        console.log(req.body)
        // 调用service模块更新 数据
        const result = await userService.getList(req.params)
        res.send({
            ActionType: 'ok',
            data: result
        })

    },
    delList: async (req, res) => {
        console.log(req.params.id)
        const result = await userService.delList({ _id: req.params.id })
        console.log(result)
        res.send({
            ActionType: 'ok',
        })
    },
    putList: async (req, res) => {
        // console.log(req.params.id)
        const result = await userService.putList(req.body)
        console.log(result)
        res.send({
            ActionType: 'ok',
        })
    }
}

module.exports = userController